package com.supermap.dialogs;

import com.supermap.Helper;
import com.supermap.data.*;
import com.supermap.data.Enum;
import com.supermap.dialogs.advance.CJDialog;
import com.supermap.realspace.*;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class StretchSettingDialog extends CJDialog {
    private JComboBox<AltitudeMode> altitudeModeCmb;
    private JComboBox<String> bottomAltitudeCmb;
    private JComboBox<String> extendedHeightCmb;
    private JComboBox<String> layer3DsCmb;

    private Scene scene;

    public StretchSettingDialog(Frame owner, String title, boolean modal, Scene scene) {
        super(owner, title, modal, 360, 180, new Object[]{scene});
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.scene = (Scene) args[0];
    }

    @Override
    protected void initializeCore() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        super.confirmBtn.setText("应用");

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());
        add(mainPanel, BorderLayout.CENTER);

        JLabel layerLbl = new JLabel("图层: ");
        mainPanel.add(layerLbl, new GridBagConstraints(0, 0, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        layer3DsCmb = new JComboBox<>();
        if (scene != null) {
            Layer3Ds layers = scene.getLayers();
            if (layers != null && layers.getCount() > 0) {
                for (int i = 0; i < layers.getCount(); i++) {
                    Layer3D layer3D = layers.get(i);
                    if (layer3D.getType() == Layer3DType.DATASET) {
                        layer3DsCmb.addItem(layer3D.getName());
                    }
                }
            }
        }
        mainPanel.add(layer3DsCmb, new GridBagConstraints(1, 0, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        JLabel altitudeModeLbl = new JLabel("高度模式: ");
        mainPanel.add(altitudeModeLbl, new GridBagConstraints(0, 1, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        altitudeModeCmb = new JComboBox<>();
        Enum[] enums = AltitudeMode.getEnums(AltitudeMode.class);
        for (Enum anEnum : enums) {
            if (anEnum instanceof AltitudeMode) {
                altitudeModeCmb.addItem((AltitudeMode) anEnum);
            }
        }
        altitudeModeCmb.addItemListener(e -> {
            AltitudeMode altitudeMode = (AltitudeMode) e.getItem();
            if (altitudeMode.equals(AltitudeMode.CLAMP_TO_GROUND)) {
                bottomAltitudeCmb.setEnabled(false);
                extendedHeightCmb.setEnabled(false);
            } else {
                bottomAltitudeCmb.setEnabled(true);
                extendedHeightCmb.setEnabled(true);
            }
        });
        mainPanel.add(altitudeModeCmb, new GridBagConstraints(1, 1, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        JLabel bottomAltitudeLbl = new JLabel("底部高程: ");
        mainPanel.add(bottomAltitudeLbl, new GridBagConstraints(0, 2, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        ArrayList<String> fieldNames = null;
        if (layer3DsCmb.getItemCount() > 0) {
            fieldNames = getFieldNames((String) layer3DsCmb.getSelectedItem());
        }

        bottomAltitudeCmb = new JComboBox<>();
        if (fieldNames != null) {
            for (String fieldName : fieldNames) {
                bottomAltitudeCmb.addItem(fieldName);
            }
        }
        mainPanel.add(bottomAltitudeCmb, new GridBagConstraints(1, 2, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        JLabel extendedHeightLbl = new JLabel("拉伸高度: ");
        mainPanel.add(extendedHeightLbl, new GridBagConstraints(0, 3, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        extendedHeightCmb = new JComboBox<>();
        if (fieldNames != null) {
            for (String fieldName : fieldNames) {
                extendedHeightCmb.addItem(fieldName);
            }
        }
        mainPanel.add(extendedHeightCmb, new GridBagConstraints(1, 3, 1, 1, 10, 1,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH, Helper.DefaultInsets, 0, 0));

        if (altitudeModeCmb.getItemCount() > 0) {
            altitudeModeCmb.setSelectedIndex(0);
            bottomAltitudeCmb.setEnabled(false);
            extendedHeightCmb.setEnabled(false);
        }
    }

    @Override
    protected void doConfirmCore() {
        if (scene == null || layer3DsCmb.getItemCount() == 0 || bottomAltitudeCmb.getItemCount() == 0 || extendedHeightCmb.getItemCount() == 0) {
            return;
        }

        setVisible(false);
        String selectedLayer3DName = (String) layer3DsCmb.getSelectedItem();
        AltitudeMode selectedAltitudeMode = (AltitudeMode) altitudeModeCmb.getSelectedItem();
        String selectedBottomAltitudeField = (String) bottomAltitudeCmb.getSelectedItem();
        String selectedExtendedHeightField = (String) extendedHeightCmb.getSelectedItem();

        Layer3Ds layer3Ds = scene.getLayers();
        Layer3DDataset layer3DDataset = (Layer3DDataset) layer3Ds.get(selectedLayer3DName);
        Layer3DSettingVector layer3DSettingVector = (Layer3DSettingVector) layer3DDataset.getAdditionalSetting();
        layer3DSettingVector.setBottomAltitudeField(selectedBottomAltitudeField);
        layer3DSettingVector.setExtendedHeightField(selectedExtendedHeightField);
        GeoStyle3D geoStyle3D = layer3DSettingVector.getStyle();
        geoStyle3D.setAltitudeMode(selectedAltitudeMode);
        geoStyle3D.setFillMode(FillMode3D.LINE_AND_FILL);

        layer3DDataset.updateData();
        scene.refresh();
    }

    private ArrayList<String> getFieldNames(String layerName) {
        ArrayList<String> fieldNames = new ArrayList<>();
        if (scene != null) {
            Layer3Ds layers = scene.getLayers();
            if (layers != null && layers.getCount() > 0) {
                String[] dataNames = layerName.split("@");
                if (dataNames.length == 2) {
                    Datasource datasource = scene.getWorkspace().getDatasources().get(dataNames[1]);
                    if (datasource != null) {
                        Dataset dataset = datasource.getDatasets().get(dataNames[0]);
                        if (dataset instanceof DatasetVector) {
                            DatasetVector datasetVector = (DatasetVector) dataset;
                            FieldInfos fieldInfos = datasetVector.getFieldInfos();
                            for (int i = 0; i < fieldInfos.getCount(); i++) {
                                fieldNames.add(fieldInfos.get(i).getName());
                            }
                        }
                    }
                }
            }
        }
        return fieldNames;
    }
}
